#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static bool prime(uint64_t n) {
    if (n == 2)
        return true;
    if (n % 2 == 0)
        return false;
    if (n < 2)
        return false;
    for (uint64_t i = 3; i * i <= n; i += 2)
        if (n % i == 0)
            return false;
    return true;
}

static bool pandigital(uint64_t n) {
    vector<int> digits {0};
    while (n) {
        digits.push_back(n % 10);
        n /= 10;
    }
    sort(digits.begin(), digits.end());
    for (int i = 0; i < digits.size(); ++i)
        if (digits[i] != i)
            return false;
    return true;
}

int main() {
    for (uint64_t i = 7654321; ; i -= 2) {
        if (pandigital(i) && prime(i)) {
            cout << i << endl; // 7652413
            break;
        }
    }
}